package com.luyun.frogpay.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 描述：
 *
 * @author 无人区
 * @date 2019/4/3
 */
public class TimeUtil {

    /**
     * 比较两个日期
     * @param date1
     * @param date2
     * @return
     */
    public static int compareDays(Date date1, Date date2) {
        return date1.compareTo(date2);
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //不同一年
        //前小后大
        if (year1 < year2) {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                }
                //不是闰年
                else {
                    timeDistance += 365;
                }
            }
            if (day1 < day2) {
                return day1 + timeDistance - day2;
            } else {
                return day2 + timeDistance - day1;
            }
        }
        //前大后小
        else if (year1 > year2) {
            int timeDistance = 0;
            for (int i = year2; i < year1; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                }
                //不是闰年
                else {
                    timeDistance += 365;
                }
            }
            if (day1 < day2) {
                return (day1 + timeDistance) - day2;
            } else {
                return timeDistance + (day1 - day2);
            }
        }
        //同一年
        else {
            if (day1 < day2) {
                return day2 - day1;
            } else {
                return day1 - day2;
            }
        }
    }

    /**
     * 将一天中的分钟计时转换为 时：分 格式
     *
     * @param minutes hh:mm
     * @return
     */
    public static String formatMinutesToDateStr(int minutes) {
        int hours = (int) Math.floor(minutes / 60);
        int minute = minutes % 60;
        String minuteStr;
        if (minute < 10){
            minuteStr = "0" + minute;
        }else {
            minuteStr = "" + minute;
        }
        return hours + ":" + minuteStr;
    }

    /**
     * 将分钟计时转换为 *h*m 格式
     *
     * @param minutes *h:*m
     * @return
     */
    public static String formatMinutesToHours(int minutes) {
        int hours = (int) Math.floor(minutes / 60);
        int minute = minutes % 60;
        return hours + "h" + minute + "m";
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     * @param format  如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 日期字符串转日期类型
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date dateStrToDate(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 年月日转日期类型
     *
     * @param
     * @param format
     * @return
     */
    public static Date dateYMDToDate(int year, int month, int day, String format) {
        String dateStr = year + "-" + month + "-" + day + "-";
        return TimeUtil.dateStrToDate(dateStr, "yyyy-MM-dd");
    }

    /**
     * 日期类型转日期字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static Date dateToDateStr(Date date, String format) {
        long timeStamp = date.getTime();
        return new Date(timeStamp);
    }

    /**
     * 20190626  格式转换为年月日格式
     * @param dateNumber  20190626
     * @return
     */
    public static String formattedDate(long dateNumber){
        String dateStr = String.valueOf(dateNumber);
        String date = dateStr.substring(0,4) + "年" + dateStr.substring(4,6) + "月" + dateStr.substring(6) + "日";
        return date;
    }



    /**
     * 20190626  格式转换为年月日格式
     * @param dateNumber  20190626
     * @return
     */
    public static String formattedDateWithNoUnit(long dateNumber){
        String dateStr = String.valueOf(dateNumber);
        String date = dateStr.substring(0,4) + "-" + dateStr.substring(4,6) + "-" + dateStr.substring(6);
        return date;
    }
}
